<template>
    <view class="list">
        <form @submit="formSubmit">
            <view class="item">
                <label>收货人</label>
                <input :value="address != '' ? address.name : ''" name="name" placeholder="名字" />
            </view>
            <view class="item">
                <label>手机号</label>
                <input :value="address != '' ? address.tel : ''" type="idcard" name="tel" placeholder="11位手机号" />
            </view>
            <view class="item">
                <label>详细地址</label>
                <input :value="address != '' ? address.address : ''" name="address" placeholder="详细地址" />
            </view>
            <view class="item bt">
                <button formType="submit" class="btn">{{ address.address != '' ? '修改' : '添加' }}</button>
                <block v-if="address.address != ''">
                    <button @tap="deleteAddress" class="btn">删除</button>
                </block>
            </view>
        </form>
    </view>
</template>

<script>
var config = require('../../../config.js');
var http = require('../../../utils/httpHelper.js');
export default {
    data() {
        return {
            userInfo: {},
            address: {
                name: '',
                tel: '',
                address: ''
            }
        };
    }, //事件处理函数
    onLoad: function (options) {
        var that = this;
        let userInfo = uni.getStorageSync('user');
        that.setData({
            userInfo: userInfo
        });
        //添加地址
        var id = options.id;
        if (id != '') {
            var data = {
                id: id
            };
            http.httpGet(config.clubApi.getAddrInfo, data, function (res) {
                if (res.code == '200') {
                    var address = res.data;
                    that.setData({
                        address: {
                            id: id,
                            name: address.name,
                            address: address.address,
                            tel: address.phone
                        }
                    });
                }
            });
        }
    },
    methods: {
        formSubmit: function (e) {
            //提交表单
            var val = e.detail.value;
            if (this.address != '') {
                var data = {
                    id: this.address.id,
                    name: val.name,
                    phone: val.tel,
                    address: val.address
                };
                http.httpPut(config.clubApi.editAddress, data, function (res) {
                    if (res.code == '200') {
                        uni.navigateBack();
                        console.log('编辑地址成功');
                    } else {
                        //wx.navigateBack();
                        console.log('编辑地址失败');
                    }
                });
            } else {
                var data = {
                    userId: this.userInfo.userId,
                    name: val.name,
                    phone: val.tel,
                    address: val.address
                };
                http.httpPut(config.clubApi.addAddress, data, function (res) {
                    if (res.code == '200') {
                        uni.navigateBack();
                        console.log('添加地址成功');
                    } else {
                        //wx.navigateBack();
                        console.log('添加地址失败');
                    }
                });
            }
        },

        deleteAddress: function () {
            if (this.address != '' && this.address.id != '') {
                var id = this.address.id;
                var data = {
                    id: id
                };
                http.httpPut(config.clubApi.delAddress, data, function (res) {
                    if (res.code == '200') {
                        uni.showToast({
                            title: '删除地址成功！',
                            icon: 'success',
                            duration: 500
                        });
                        uni.navigateBack();
                        console.log('删除地址成功');
                    } else {
                        //wx.navigateBack();
                        console.log('删除地址失败');
                    }
                });
            } else {
                console.log('删除地址失败');
            }
        }
    }
};
</script>
<style lang="scss">
@import './index.scss';
</style>
